#pragma once
#pragma comment( lib, "strsafe.lib" )

#include <strsafe.h>

#include "STLUtil.h"
#include "TimerQueue.h"
#include "HTTPMgr.h"

static const INT_PTR CHECK_TIMER_ID	= ( WM_USER + 1108 );

typedef struct _CheckINFO
{
	DWORD				dwCycle;
	deque< tstring >	dequeKeyWords;
	INT_PTR				nLastID;

	_CheckINFO( VOID ) :
		dwCycle( NULL ),
		nLastID( NULL )
	{
	}

	_CheckINFO( DWORD dwCycle, deque< tstring > & dequeKeyWords ) :
		nLastID( NULL )
	{
		this->dwCycle = dwCycle;
		ITERATE( deque< tstring >, this->dequeKeyWords, iterKeyWords )
			this->dequeKeyWords.push_back( *iterKeyWords );
	}

	BOOL Initialize( DWORD dwCycle, deque< tstring > & dequeKeyWords )
	{
		this->dequeKeyWords.clear();		
		this->nLastID = 0;
		this->dwCycle = dwCycle;
		ITERATE( deque< tstring >, this->dequeKeyWords, iterKeyWords )
			this->dequeKeyWords.push_back( *iterKeyWords );

		return this->dwCycle ? TRUE : FALSE;
	}

} CheckINFO;

typedef struct _ArticleINFO
{
	INT_PTR	nID;
	tstring	sTitle;

	_ArticleINFO( VOID ) :
		nID( 0 )
	{
	}

	_ArticleINFO( INT_PTR nID, tstring & sTitle )
	{
		this->nID = nID;
		this->sTitle.assign( sTitle.c_str() );
	}

	_ArticleINFO( const _ArticleINFO & articleINFO )
	{
		this->nID = articleINFO.nID;
		this->sTitle.assign( articleINFO.sTitle.c_str() );
	}

} ArticleINFO;

typedef struct _ArticleList
{
	_ArticleList( VOID )
	{
	}

	~_ArticleList( VOID )
	{
		this->CleanUp();
	}

	VOID CleanUp( VOID )
	{
		this->dequeArticleINFO.clear();
	}

	BOOL Add( INT_PTR nID, tstring & sTitle )
	{
		ArticleINFO articleINFO( nID, sTitle );
		
		return this->Add( articleINFO );
	}
	
	BOOL Add( ArticleINFO articleINFO )
	{
		this->dequeArticleINFO.push_back( articleINFO );

		return TRUE;
	}

	deque< ArticleINFO >	dequeArticleINFO;

} ArticleList;

class CNaverCafeKeyWordMonitor
{
	public	:
		
				CNaverCafeKeyWordMonitor( VOID );
		virtual	~CNaverCafeKeyWordMonitor( VOID );

		BOOL	Initialize( DWORD dwCycle, deque< tstring > & dequeKeyWords );

		BOOL	DoCheck( VOID );

		static 
		VOID	TimerCallBackProc( INT_PTR nID, DWORD_PTR dwCallBackReceiver );
		BOOL	TimerCallBack( INT_PTR nID );

	private	:

		CheckINFO	m_CheckINFO;
		CHTTPMgr	m_HTTPMgr;
		CTimerQueue	m_TimerQueue;
		ArticleList	m_ArticleList;

		BOOL	CheckNow( VOID );
		BOOL	CheckNow( ArticleList * pArticleList, LPCTSTR pszURL );
		BOOL	Parse( ArticleList * pArticleList, tstring &sContents );
		BOOL	Parse( tstring & sOri, tstring::size_type & nOffSet, LPCTSTR pszFront, LPCTSTR pszEnd, tstring & sParsed );

		BOOL	IsAbleToStopToCheck( ArticleList * pArticleList, INT_PTR & nLastID );
};
